Some Program Codes
Program 1:
//Do math according to user choice import java.util.*; class basic_num{ public static void main(String[] args){ //relational operators: >, >=, <=, ==,<, != int x=300_200,y=5; System.out.print("First: "+args[0]); Scanner s = new Scanner(System.in); String ch = s.next(); System.out.print("ch: "+ch); switch(ch){ case "+":System.out.print(x+y); break; case "*":System.out.print(x+y); break; } } }Program 2:
//box class with length, breadth,height, area, volume //methods to calculate area, volume /* * box --> l, b, h, area, volume */ class box_a{ private int l,b,h; //private int b; //private int h; //member variables public int area; public int volume; public void set(int l,int b, int h){ this.l = l; this.b = b; this.h = h; } public int getArea(){ area = l * b; return area; } public int getVolume(){ volume = l * b * h; return volume; } } class box_demo_i{ public static void main(String[] para){ box_a b1 = new box_a(); b1.set(2,3,4);//l = 2, b = 3, h = 4; System.out.println("Area: "+b1.getArea()); System.out.println("Volume: "+b1.getVolume()); box_a b2 = new box_a(); b2.set(4,5,7); b1.h = 3; System.out.println("Area: "+b2.area); System.out.println("Volume: "+b2.volume); } }Program 3:
//using Command line argument class command_args{ public static void main(String[] aa){ System.out.println("Elements count: "+aa.length); int x = Integer.parseInt(aa[0]); int y = Integer.parseInt(aa[1]); int sum = x + y; if(x % 2 == 0 ){ System.out.println(x+ ": Even"); }else{ System.out.println(x+ ": Odd"); } System.out.println("Sum: "+sum); for(int i=0;i<aa.length;i++){ System.out.println(i+": "+aa[i]); } } }Program 4:
//Performing Dynamic Polymorphism class person{ int id; String name; public person(int id,String name){ this.id = id; this.name = name; } public void show(){ System.out.println("ID: "+id+"\nName: "+name); } } class student extends person{ String school; public student(int age, String name,String school){ super(age, name); this.school = school; } public void show(){//method overriding //based class method is redefined by derived class. super.show(); System.out.println("School: "+school); } } class dyna_ploy_demo{ public static void main(String[] oo){ person p = new person(1,"Dinesh"); p.show(); p = new student(2,"Sheetal","AAMV"); //based class object can take reference of child class. p.show();//student has show, person also has show method. } }Program 5:
/*Dynamic Polymorphism in Java (also known as runtime polymorphism) is a core concept of object-oriented programming where method calls are resolved at runtime rather than compile-time. It refers to the ability of a program to decide which method implementation to execute at runtime, rather than at compile time. */ /*bubble sorting: sorting list of data in order (ascending or descending)*/ class fib_series{ public static void main(String[] aa){ int[] arr = {33,22,1,4,6,7,5,99,10}; int i,j,temp,n = arr.length; for(i=0;i<n;i++){ for(j=0;j<n;j++){ if(arr[j+1]<arr[j]){ temp = arr[j+1]; arr[j+1] = arr[j]; arr[j] = temp; } System.out.print(" "+arr[j]); } System.out.println(); } for(i=0;i<n;i++){ System.out.print(" "+arr[i]); } } } /* * a = 4, b = 5; c = a, a = b, b = c * c = 4, a = 5 ; b = 4 */Program 6:
interface abc{ void show(); } interface xyz{ void sayHello(); } class C1 implements abc,xyz{ public void show(){ System.out.println("Hello from Interface"); } public void sayHello(){ System.out.println("Hello said"); } } class InterfaceDemo{ public static void main(String[] arr){ C1 c = new C1(); c.show(); c.sayHello(); abc x = new C1(); x.show(); //x.sayHello(); xyz z = new C1(); z.sayHello(); } }Program 7:
/* In Java, an interface is a reference type, similar to a class, that can contain only: - Abstract methods (implicitly public and abstract) - Default methods (with a body, using default) - Static methods - Constant fields (public static final) Purpose of Interfaces - Define a contract that implementing classes must follow. - Support multiple inheritance (Java doesn't allow multiple class inheritance, but a class can implement multiple interfaces). - Enable polymorphism and abstraction. AFTER JAVA 8, methods in interface can have definition interface MyInterface { default void show() { System.out.println("Default implementation"); } } interface MyInterface { static void display() { System.out.println("Static method in interface"); } } interface MyInterface { int VALUE = 10; // implicitly public, static, final } When to Use Interfaces When you need a common functionality contract for unrelated classes.*/Program 8:
//Calculating value of a number powered by some number. class power{ public static void main(String[] para){ int x=3,y =5; int p = x; for(int i=2;i<=y;i++){ p = p * x; } System.out.print("Power of "+x+" is: "+p); } }